package util;

import java.util.ArrayList;
import java.util.Iterator;

public class FMenu {
	
	public static ArrayList<Character> listainv(){
		ArrayList<Character> list = new ArrayList<Character>();				
		list.add('&');
		list.add('<');
		list.add('>');
		list.add((char)145);//'
		list.add('"');
		return list;
	}

	public static ArrayList<String> listaval(){
		ArrayList<String> list = new ArrayList<String>();				
		list.add("&amp;");
		list.add("&lt;");
		list.add("&gt;");
		list.add("&apos;");
		list.add("&quot;");
		list.add("&frasl;");//  /
		list.add("&minus;");//  -
		list.add("&#43;");//    + 
		list.add("&#42;");//    *
		return list;
	}
	
	////////////////////caracteres invalidos/////////////////
	public static String invalidos(String caract){
		ArrayList<Character> listin = listainv();
		int j = 0;
		for (Iterator<Character> iter = listin.iterator(); iter.hasNext();) {
			Character element = iter.next();
			if(element == (char) caract.hashCode()){
				ArrayList<String> listva = listaval();
				return listva.get(j);
			}
			j = j + 1; 
		}
		return caract;
	}
	
	////////////////////Validar campo xml/////////////////
	public static String valida(String campo){
		StringBuffer aux = new StringBuffer("");
		String ret = "";
		if(campo != null){
			for (int i = 0; i < (campo.length()); i++) {
				aux.append(invalidos(campo.substring(i, i+1)));    
			}
			ret = aux.toString();
			if(ret.trim().length() == 0){
				ret = "...";		
			}
		}else{
			ret = "...";
		}
		return ret;
	}

	////////////////////inicio arquivo XML/////////////////
	public static String incxml(){
		StringBuffer aux = new StringBuffer("");
		aux.append("<?xml version='1.0'?> ");
		aux.append("<menu>");
		return aux.toString();
	}

	////////////////////Final arquivo XML/////////////////
	public static String finxml(){
		StringBuffer aux = new StringBuffer("");		
		aux.append("</menu>");
		return aux.toString();
	}

	////////////////////Campo arquivo XML/////////////////
	public static String campxml(String id, String valor){		
		StringBuffer aux = new StringBuffer("");
		String val = valida(valor);
		aux.append("<campo>");
		aux.append("<id>"+id+"</id>");
		aux.append("<valor>"+val+"</valor>");
		aux.append("</campo>");
		return aux.toString();
	}

////////////////////Celula Tabela arquivo XML/////////////////
	public static String tabcelxml(String id, String valor){		
		StringBuffer aux = new StringBuffer("");
		String val = valida(valor);		
		aux.append("<"+id+">"+val+"</"+id+">");
		return aux.toString();
	}

	
////////////////////No Tabela arquivo XML/////////////////
	public static String treenode(String id, String idpai, String text, String folha, String fav){		
		StringBuffer aux = new StringBuffer("");
		String val = valida(text);
		String cls = "folder";
		String nodeType = "";
		if(folha.equalsIgnoreCase("N")){
			folha = "false";
			nodeType = ",'nodeType':'async'";
		}else{
			folha = "true";
			if(fav!=null && fav.equalsIgnoreCase("S")){
			    cls = "x-tree-node-star";
			    nodeType = ",'allowChildren':false,'favorito':true";
			}else{
				cls = "file";
				nodeType = ",'allowChildren':false";
			}
		}
		aux.append("{'id':'"+id+"','text':'"+val+"','leaf':"+folha+",'cls':'"+cls+"','iconCls':'"+cls+"'"+nodeType+"}");
		return aux.toString();
	}
	
////////////////////Inicia Modulos/////////////////
	public static String abreModulos(String texto){
		if(texto==null) texto = "<modulos>";
		else texto = "<modulos>"+valida(texto);;
		return texto;
	}
	
////////////////////abre a tag mod/////////////////
	public static String abreMod(String texto){
		return "<mod>"+valida(texto);
	}
	
////////////////////Abrir deskmod/////////////////
	public static String abreDeskMod(String texto){
		return "<deskmod>"+valida(texto);
	}
	
////////////////////fechar ta g xml/////////////////
	public static String fechaModulos(){
		return "</modulos>";
	}
	
////////////////////fechar ta g xml/////////////////
	public static String fechaMod(){
		return "</mod>";
	}
////////////////////fechar ta g xml/////////////////
	public static String fechaDeskMod(){
		return "</deskmod>";
	}
	
}
